package letcode2841

func MaxSum(nums []int, m int, k int) int64 {

	hashMap := map[int]int{}
	ans, sum := 0, 0

	for _, x := range nums[:k-1] {
		hashMap[x]++ //创建窗口的hashmap，key值单一
		sum += x
	}

	for i, in := range nums[k-1:] {
		sum += in
		hashMap[in]++

		if len(hashMap) >= m && ans < sum {
			ans = sum
		}
		out := nums[i]
		sum = sum - out

		hashMap[out]--

		if hashMap[out] == 0 {
			delete(hashMap, out)
		}

	}

	return int64(ans)
}
